package com.bobamrz.service;

import java.util.List;

import com.bobamrz.dao.DocCatalogDAO;
import com.bobamrz.domain.DocCatalog;

public class CatalogueService {
	
	private DocCatalogDAO docCatalogDAO = new DocCatalogDAO();
	
	public List<DocCatalog> getSuperCatalogs() {
		List<DocCatalog> catalogs = docCatalogDAO.getSuperDocCatalogList();
		return catalogs;
	}
	
	public String getCatalogTreeHtml() {
		List<DocCatalog> catalogs = getSuperCatalogs();
		StringBuilder sb = new StringBuilder();
		sb.append(
				"<ul><li><p>ROOT</p>");
		appendListElementContentHtml(sb, catalogs);
		sb.append("</li></ul>");
		return sb.toString();
	}
	
	/**
	 * @param sb stringbuilder to append to.
	 * @param catalogs
	 * @return
	 */
	private void appendListElementContentHtml(StringBuilder sb, Iterable<DocCatalog> catalogs) {
		sb.append("<ul>");
		for(DocCatalog catalog : catalogs) {
			sb.append("<li><p>");
			sb.append("<a href='documentView?catalog=" + catalog.getDocCatalog() + "' >");
			sb.append(catalog.getName());
			sb.append("</a>");
			sb.append("</p>");
			if(catalog.getDocCatalogs() != null) {
				appendListElementContentHtml(sb, catalog.getDocCatalogs());
			}
			sb.append("</li>");
		}
		sb.append("</ul>");
		
	}

	public DocCatalogDAO getDocCatalogDAO() {
		return docCatalogDAO;
	}

	public void setDocCatalogDAO(DocCatalogDAO docCatalogDAO) {
		this.docCatalogDAO = docCatalogDAO;
	}

}
